package utilities;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * This scans the necessary Tower files and builds any towers needed based on the
 * file information provided to the factory. By default the Tower Factory builds
 * tower "blueprints" based in the file type {@value 
 * utilities.FileFilter#DEFAULT_EXT_TOWER}. <br /><br />
 * 
 * All information needed for each tower. This information includes the cost to 
 * build/upgrade the tower, the base size (it's a square base for easier 
 * computations), tower range (radius) at certain levels,and images for the base 
 * and rotating head. The projectile it fires will have it's own definitions 
 * defined in it's own projectile.mtp 	file (default projectile file). The 
 * image file will be defined in the default Images folder. This will only specify 
 * which file will be loaded as the image to use. An example tower file is as 
 * follows: <br /> 
 * <code>
 * &lt;towers&gt; <br /> 
 *  &nbsp;&nbsp;&lt;tower type="ExampleTower"&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;cost type="build"&gt;50&lt;/cost&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;cost type="upgrade"&gt;55&lt;/cost&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;size&gt;10&lt;/size&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;range level="1"&gt;50&lt;/range&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;range level="2"&gt;60&lt;/range&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;range level="3"&gt;70&lt;/range&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;range level="4"&gt;80&lt;/range&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;range level="5"&gt;90&lt;/range&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;image part="base"&gt;example_base.img&lt;/image&gt; <br /> 
 * 		&nbsp;&nbsp;&nbsp;&nbsp;&lt;image part="head"&gt;example_head.img&lt;/image&gt; <br /> 
 * 	&nbsp;&nbsp;&lt;/tower&gt; <br /> 
 * &lt;/towers&gt; <br /> 
 * </code>
 * 
 * @author Guillermo Mendez-Kestler
 *
 */
public class Tower_Factory implements XML_Reader {

	/**
	 * Array of all tower Files needed to be parsed. 
	 */
	private File[] files;

	/**
	 * Creates a XML reader in the default directory using the 
	 * default file extension. 
	 */
	public Tower_Factory() {
		File folder = new File(DEFAULT_DIRECTORY);
		folder.getName();
		folder.toString();
		files = folder.listFiles(new FileFilter(FileFilter.DEFAULT_EXT_TOWER));
	}
	
	/**
	 * Creates a XML reader in the given directory specified by 
	 * {@link java.io.File#File(String)}. 
	 * 
	 * @param folder The folder where the XML file is to be 
	 * parsed from. 
	 */
	public Tower_Factory(File folder){
		files = folder.listFiles(new FileFilter(FileFilter.DEFAULT_EXT_TOWER));
	}
	
	/**
	 * Creates a XML reader in the given directory specified by 
	 * {@link java.io.File#File(String)} with the supplied 
	 * extension. 
	 * 
	 * @param folder
	 * @param ext
	 */
	public Tower_Factory(File folder, String ext){
		files = folder.listFiles(new FileFilter(ext));
	}
	
	/**
	 * Creates a XML reader in the given directory with the 
	 * supplied extension. 
	 * 
	 * @param folderPath The path where the file is located. 
	 * @param ext The extension to check. 
	 */
	public Tower_Factory(String folderPath, String ext) {
		File folder = new File(folderPath);
		files = folder.listFiles(new FileFilter(ext));
	}
	
	/*
	 * (non-Javadoc)
	 * @see utilities.XML_Reader#read()
	 */
	public boolean read(){
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc;
			Element element;
			NodeList towers;
			
			for (File file:files){
				if (!file.exists()){
					System.out.println("File: "+file.getName()+" does not exist.");
					continue;
				}
				doc = db.parse(file);
				towers = doc.getElementsByTagName("tower");
				for (int i=0;i<towers.getLength();i++){
					element = (Element) towers.item(i);
					element.getAttribute("type");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
